package com.cdream.basic.algorithm.leetcode.findElement;

import java.util.HashSet;
import java.util.Set;

/**
 * @author cdream
 * @date 5/30/21
 */
public class Question219 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Set<Integer> sets = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (sets.contains(nums[i])) {
                return true;
            }
            sets.add(nums[i]);
            if (sets.size() == k + 1) {
                sets.remove(nums[i - k]);
            }
        }
        return false;
    }
}